package com.mindskip.zdw.configuration.spring.security;

import com.mindskip.zdw.base.SystemCode;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
 * 段代码创建了一个名为RestAuthenticationFailureHandler的身份验证失败处理程序，
 * 它继承自SimpleUrlAuthenticationFailureHandler并重写了onAuthenticationFailure方法。
 * 当身份验证过程中发生错误时，该方法将使用RestUtil.response方法构造一个HTTP响应，
 * 并返回异常的错误消息和一个代表认证错误的系统状态码(SystemCode.AuthError)。
 *
 * 请注意，这个类只是负责处理身份验证过程中出现的错误，
 */
@Component
public class RestAuthenticationFailureHandler extends SimpleUrlAuthenticationFailureHandler {

    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
        RestUtil.response(response, SystemCode.AuthError.getCode(), exception.getMessage());
    }
}
